package com.feib.soeasy.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Hibernate;
import org.springframework.stereotype.Service;

import com.feib.soeasy.dao.UserDao;
import com.feib.soeasy.dao.UserDao.ROLE_CONSTRAINT;
import com.feib.soeasy.model.Group;
import com.feib.soeasy.model.Role;
import com.feib.soeasy.model.User;
import com.feib.soeasy.pagination.PaginatedList;
import com.feib.soeasy.service.UserIdNotFoundException;
import com.feib.soeasy.service.UserService;

/**
 * @title (#)UserServiceImpl.java<br>
 * @description <br>
 * @author Jimmy Liu<br>
 * @version 1.0.0 2010/11/25
 * @copyright Far Eastern International Bank Copyright (c) 2010<br>
 * @2010/10/26 create by Jimmy Liu<br>
 */
@Service("userService")
public class UserServiceImpl extends BaseServiceImpl implements UserService
{

    @Resource(name = "userDao")
    private UserDao userDao;

    /**
     * 登入帳號是否重複，不管 locked or enabled 與 expiredDate
     * 
     * @param userId
     *            登入帳號
     * @param groupNo
     *            群組/帳單業者代號
     * @return
     */
    @Override
    public boolean isDuplicateUserId(String userId, String groupNo)
    {
        if(userDao.findByUerIdGroupNo(userId, groupNo) != null)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    /**
     * 新增使用者
     * 
     * @param user
     * @return
     */
    @Override
    public User addUser(User user)
    {

        return userDao.save(user);
    }

    @Override
    public User saveUser(User user)
    {
        return userDao.save(user);
    }

    /**
     * 以登入帳號與群組代號取得使用者資訊
     * 
     * @param userId
     *            登入帳號
     * @param groupNo
     *            群組/帳單業者代號
     * @return
     */
    @Override
    public User queryByUerIdGroupNo(String userId, String groupNo)
    {
        return userDao.findByUerIdGroupNo(userId, groupNo);
    }

    /**
     * 以始用者主鍵取得使用者資訊
     * 
     * @param userPk
     *            登入帳號主鍵
     * @return
     */
    @Override
    public User queryByUerPk(Long userPk)
    {
        return userDao.findByPk(userPk);
    }

    /**
     * 啟用使用者帳號(set enabled property to TRUE)
     * 
     * @param userId
     *            登入帳號
     * @param groupNo
     *            群組/帳單業者代號
     * @return
     * @throws UserIdNotFoundException
     */
    @Override
    public User enableUser(String userId, String groupNo) throws UserIdNotFoundException
    {
        User user = userDao.findByUerIdGroupNo(userId, groupNo);

        user.setEnabled(true);

        return userDao.save(user);
    }

    /**
     * 啟用使用者帳號(set enabled property to TRUE)
     * 
     * @param userPk
     *            登入帳號主鍵
     * @return
     */
    @Override
    public User enableUser(Long userPk)
    {
        User user = userDao.findByPk(userPk);

        user.setEnabled(true);

        return userDao.save(user);
    }

    /**
     * 停用使用者帳號(set enabled property to FALSE)
     * 
     * @param userId
     *            登入帳號
     * @param groupNo
     *            群組/帳單業者代號
     * @return
     * @throws UserIdNotFoundException
     */
    @Override
    public User disableUser(String userId, String groupNo) throws UserIdNotFoundException
    {
        User user = userDao.findByUerIdGroupNo(userId, groupNo);

        user.setEnabled(false);

        return userDao.save(user);
    }

    /**
     * 停用使用者帳號(set enabled property to FALSE)
     * 
     * @param userPk
     *            登入帳號主鍵
     * @return
     */
    @Override
    public User disableUser(Long userPk)
    {
        User user = userDao.findByPk(userPk);

        user.setEnabled(false);

        return userDao.save(user);
    }

    /**
     * 鎖定使用者帳號(set locked property to True)
     * 
     * @param userId
     *            登入帳號
     * @param groupNo
     *            群組/帳單業者代號
     * @return
     * @throws UserIdNotFoundException
     */
    @Override
    public User lockUser(String userId, String groupNo) throws UserIdNotFoundException
    {
        User user = userDao.findByUerIdGroupNo(userId, groupNo);

        user.setLocked(true);

        return userDao.save(user);
    }

    /**
     * 鎖定使用者帳號(set locked property to True)
     * 
     * @param userPk
     *            登入帳號主鍵
     * @return
     */
    @Override
    public User lockUser(Long userPk)
    {
        User user = userDao.findByPk(userPk);

        user.setLocked(true);

        return userDao.save(user);
    }

    /**
     * 解鎖定使用者帳號(set locked property to FALSE)
     * 
     * @param userId
     *            登入帳號
     * @param groupNo
     *            群組/帳單業者代號
     * @return
     * @throws UserIdNotFoundException
     */
    @Override
    public User unLockUser(String userId, String groupNo) throws UserIdNotFoundException
    {
        User user = userDao.findByUerIdGroupNo(userId, groupNo);

        user.setLocked(false);

        return userDao.save(user);
    }

    /**
     * 解鎖定使用者帳號(set locked property to FALSE)
     * 
     * @param userPk
     *            登入帳號主鍵
     * @return
     */
    @Override
    public User unLockUser(Long userPk)
    {
        User user = userDao.findByPk(userPk);

        user.setLocked(false);

        return userDao.save(user);
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * com.feib.soeasy.service.UserService#loginByUerIdGroupNo(java.lang.String,
     * java.lang.String)
     */
    @Override
    public User loginByUerIdGroupNo(String userId, String groupNo)
    {
        User user = userDao.findByUerIdGroupNo(userId, groupNo);
        if(null != user)
        {
            Hibernate.initialize(user.getRoles());

            for(Role role : user.getRoles())
                Hibernate.initialize(role.getMenuItems());
        }

        return user;
    }

    @Override
    public List<User> getAllUser()
    {
        return userDao.findAll();
    }

    @Override
    public PaginatedList<User> queryUserPaginationByRole(String userId,
                    String userName,
                    String groupNo,
                    ROLE_CONSTRAINT role_constraint,
                    String roleNo,
                    PaginatedList<User> paginationConfig)
                    {
        logger.debug("Query role:{}", roleNo);

        return userDao.findByUerIdUserNameGroupNo(	userId,
                        userName,
                        groupNo,
                        role_constraint,
                        roleNo,
                        paginationConfig);
                    }

    @Override
    public List<User> findUserByGroupNo(String groupNo)
    {
        return userDao.findByGroupNo(groupNo);
    }

    /* (non-Javadoc)
     * @see com.feib.soeasy.service.UserService#findPaginationUsers(java.lang.String, java.lang.String, java.lang.String, com.feib.soeasy.pagination.PaginatedList)
     */
    @Override
    public PaginatedList<User> findPaginationUsers(String groupNo, String userId, String userName, PaginatedList<User> paginationConfig) {
        return userDao.queryPaginationUsers(groupNo, userId, userName, paginationConfig);
    }

    /* (non-Javadoc)
     * @see com.feib.soeasy.service.UserService#findPaginationForAdministratorByGroupNo(java.lang.String, com.feib.soeasy.pagination.PaginatedList)
     */
    @Override
    public PaginatedList<User> findPaginationForAdministratorByGroupNo(String groupNo, PaginatedList<User> paginationConfig) {
        return userDao.queryPaginationForAdministratorByGroupNo(groupNo, paginationConfig);
    }

    /* (non-Javadoc)
     * @see com.feib.soeasy.service.UserService#countUserByGroupExcludeAdmin(com.feib.soeasy.model.Group)
     */
    @Override
    public int countUserByGroupExcludeAdmin(Group group) {
        return this.userDao.countUserByGroupExcludeAdmin(group);
    }

}
